﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls;

using Might.UIControls.PropertyEditor.Items;

namespace Might.UIControls.PropertyEditor {

    public partial class PropertyEditorView : UserControl , IPropertyEditorView {

        private PropertyEditorPresenter presenter;
        private Dictionary< string , StackPanel > groups = new Dictionary<string,StackPanel>();
        

        public event EventHandler ItemChanged;              
        
        public PropertyEditorView() {
            InitializeComponent();
        }
        
        public PropertyEditorView( PropertyEditorPresenter presenter ) : this() {
            this.presenter = presenter;
            presenter.View = this;
        }
        
        public void AddItem ( string groupName , IPropertyEditorItem item ) {
            
            if( groupName == null ) {

                PropertyEditor.Children.Insert( 0 , item as UIElement );
            
            } else {

                item.ValueChanged += new EventHandler( item_ValueChanged );
            
                if( groups.ContainsKey( groupName ) ) {
                    
                    groups[ groupName ].Children.Add( item as UIElement );
                    
                } else {
                
                    Border border = new Border() { BorderThickness = new Thickness(3) , BorderBrush = new SolidColorBrush( Colors.Gray ) };                    
                    Expander expander = new Expander() { ExpandDirection = ExpandDirection.Down , Header = groupName , IsExpanded = true };                   
                    border.Child = expander;
                   
                    StackPanel stack = new StackPanel() { Orientation = Orientation.Vertical };
                    expander.Content = stack;
                                                       
                    stack.Children.Add( item as UIElement );
                    
                    PropertyEditor.Children.Add( border );
                                        
                    groups.Add( groupName , stack );
                            
                }
                
            }
            
        }                        

        public void ClearItems() {
        
            foreach( UIElement el in PropertyEditor.Children ) {                
                if( el is IPropertyEditorItem ) {
                    ( el as IPropertyEditorItem ).ValueChanged -= new EventHandler( item_ValueChanged );            
                }                                
            }
            
            foreach( KeyValuePair< string , StackPanel > key in groups ) {
                
                foreach( IPropertyEditorItem item in key.Value.Children ) {
                    item.ValueChanged -= new EventHandler( item_ValueChanged );  
                }
            
            }
            
            groups.Clear();
            
            PropertyEditor.Children.Clear();                
        }
        
        protected void onItemChanged( object sender , EventArgs e ) {
            if( ItemChanged != null ) {
                ItemChanged( sender , e );
            }
        }

        void item_ValueChanged( object sender , EventArgs e ) {
            onItemChanged( sender , null );
        }
        
    }
}
